package cn.lanqiao.ui;

import java.awt.BorderLayout;
import java.awt.Color;

/*
 * 
 * function			：登录界面
 * 
 */

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import cn.lanqiao.entity.Users;
import cn.lanqiao.service.UserService;
import cn.lanqiao.service.impl.UserServiceImpl;

public class LogonFrm extends JFrame {

	private static final long serialVersionUID = 1L;
	//北部组件
	private JLabel lblpicture ;
	
	//中间组件
	private JPanel centrePanel;
	private JLabel lblAccount;
	private JLabel lblPassword;
	private JTextField textUserName;
	private JPasswordField textUserPassword;
	private JCheckBox chckbxRememberPassword;
	
	//南部组件
	private JPanel southPanel;
	private JButton btnSouthLogon;
	private JLabel lblForgetPass;
	private JLabel lblvisitor;
	
	private String path ="rememberAP.dat";
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LogonFrm frame = new LogonFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public LogonFrm() {
		
		getContentPane().setFont(new Font("微软雅黑", Font.PLAIN, 20));
		
		//窗体北部组件——配图
		lblpicture = new JLabel();
		lblpicture.setIcon(new ImageIcon("images\\540_110.png"));
		lblpicture.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		getContentPane().add(lblpicture, BorderLayout.NORTH);
		
		//窗体中间组件——登录对话框
		centrePanel = new JPanel();
		getContentPane().add(centrePanel, BorderLayout.CENTER);
		centrePanel.setLayout(null);
		
		lblAccount = new JLabel(" 学  工  号 ");
		lblAccount.setFont(new Font("微软雅黑", Font.PLAIN, 22));
		lblAccount.setBounds(40, 40, 120, 30);
		centrePanel.add(lblAccount);
		
		textUserName = new JTextField();
		textUserName.setFont(new Font("微软雅黑", Font.PLAIN, 22));
		textUserName.setBounds(160, 40, 280, 30);
		centrePanel.add(textUserName);
		textUserName.setColumns(10);
		
		lblPassword = new JLabel("   密    码   ");
		lblPassword.setFont(new Font("微软雅黑", Font.PLAIN, 22));
		lblPassword.setBounds(40, 100, 120, 30);
		centrePanel.add(lblPassword);
		
		textUserPassword = new JPasswordField();
		textUserPassword.setFont(new Font("微软雅黑", Font.PLAIN, 22));
		textUserPassword.setBounds(160, 100, 280, 30);
		centrePanel.add(textUserPassword);
		
		chckbxRememberPassword = new JCheckBox("记住密码");
		chckbxRememberPassword.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		chckbxRememberPassword.setBounds(330, 145, 100, 30);
		centrePanel.add(chckbxRememberPassword);
		
		
		//窗体南部组件——登录按钮
		southPanel = new JPanel();
		getContentPane().add(southPanel, BorderLayout.SOUTH);
		//访客
		lblvisitor = new JLabel("访客登录");
		lblvisitor.setForeground(Color.LIGHT_GRAY);
		lblvisitor.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseEntered(MouseEvent e) {
				JLabel jla = (JLabel) e.getSource();
				jla.setForeground(Color.BLUE);
			}
			@Override
			public void mouseExited(MouseEvent e) {
				JLabel jla = (JLabel) e.getSource();
				jla.setForeground(Color.LIGHT_GRAY);
			}
			@Override
			public void mouseClicked(MouseEvent e) {
				//响应用户双击
				if(e.getClickCount()==2) {
					ForeignFrm forFrm = new ForeignFrm();
					forFrm.setVisible(true);
					LogonFrm.this.setVisible(false);
				}
			}
		});
		lblvisitor.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		southPanel.add(lblvisitor);
		//登录按钮
		btnSouthLogon = new JButton("          登          录          ");
		btnSouthLogon.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//实现登录
				//获取账号密码
				String account = textUserName.getText();
				String password = new String( textUserPassword.getPassword() );
				//验证
				UserService service = new UserServiceImpl();
				Users users = service.login(account, password);
				//账号密码错误
				if( users == null) {
					textUserName.setText(null);
					textUserPassword.setText(null);
					JOptionPane.showMessageDialog(null, "账号（密码）错误，请重新输入！");
				}
				//账号密码正确
				else {
					boolean selected = chckbxRememberPassword.isSelected();
					if( selected ) {
						//序列化（数据写入磁盘）
						rememberPassword( users );
					}
					//取消记住密码
					else {
						File file = new File( path );
						if( file.exists()) {
							file.delete();
						}
					}				
					int role = users.returnRole(users.getRole());
					switch(role) {
						case 0:
							//学生窗体
							StudentMainFrm student = new StudentMainFrm(users.getAccount());
							student.setVisible(true);
							break;
						case 1:
							//教师窗体
							TeacherMainFrm teacher = new TeacherMainFrm();
							teacher.setVisible(true);
							break;
						case 2:
							//管理员窗体
							UsersFrm usersfrm = new UsersFrm();
							usersfrm.setVisible(true);
							break;
					}
					//调用主界面
//					MainFrm mainFrm = new MainFrm( );
//					mainFrm.setVisible( true );
					//登录界面隐藏
					LogonFrm.this.setVisible(false);
				}
			}
		});
		btnSouthLogon.setFont(new Font("微软雅黑", Font.PLAIN, 22));
		southPanel.add(btnSouthLogon);
		//忘记密码
		lblForgetPass = new JLabel("忘记密码");
		lblForgetPass.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseExited(MouseEvent e) {
				JLabel jla = (JLabel) e.getSource();
				jla.setForeground(Color.LIGHT_GRAY);
			}
			@Override
			public void mouseEntered(MouseEvent e) {
				JLabel jla = (JLabel) e.getSource();
				jla.setForeground(Color.BLUE);
			}
			@Override
			public void mouseClicked(MouseEvent e) {
				if(e.getClickCount()==2) {
					CheckFrm checkFrm = new CheckFrm( );
					checkFrm.setVisible( true );
				}
			}
		});
		lblForgetPass.setForeground(Color.LIGHT_GRAY);
		lblForgetPass.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		southPanel.add(lblForgetPass);
		setFont(new Font("微软雅黑", Font.PLAIN, 20));
		setTitle("教务管理系统");
		setResizable(false);
		setBounds(100, 100, 540, 380);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//窗体居中
		setLocationRelativeTo(null);
		readFile();
	}
	
	private void rememberPassword(Users users) {
		OutputStream out = null ;
		ObjectOutputStream objout = null ;
		try {
			out = new FileOutputStream(path);
			objout = new ObjectOutputStream(out);
			objout.writeObject(users);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(objout != null ) {
				try {
					objout.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if( out != null ) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}	
	}

	private void readFile() {
		File file = new File ( path ) ;
		InputStream in = null ;
		ObjectInputStream objin = null ;
		if( file.exists()) {
			try {
				in = new FileInputStream(file);
				objin = new ObjectInputStream(in);
				Users user = (Users) objin.readObject();
				textUserName.setText(user.getAccount());
				textUserPassword.setText(user.getPassword());
				chckbxRememberPassword.setSelected(true);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}finally {
				if( objin != null ) {
					try {
						objin.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if( in != null ) {
					try {
						in.close();
					} catch (IOException e) {
						e.printStackTrace();
					} 
				}
			}
		}
	}
}
